import java.io.*;
import java.util.*;

public class Main {
	BufferedReader bf;
	StringTokenizer st;
	StringBuilder sb;
	
	final int size = 10;
	int m,
		n,
		x,
		y;
	char[][] cipherText;			
	
	public Main() {
		bf = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.beginProcess();
	}

	public void beginProcess() {
		try {
			String str;
			int i,
				j;
			
			cipherText = new char[size][size];
			for(i = 0; i < cipherText.length; i++) {
				st = new StringTokenizer(bf.readLine());
				str = st.nextToken();
				for(j = 0; j < cipherText.length; j++)
					cipherText[i][j] = str.charAt(j);
			}
			
			st = new StringTokenizer(bf.readLine());
			m = Integer.parseInt(st.nextToken());
			for(i = 0; i < m; i++) {
				st = new StringTokenizer(bf.readLine());
				n = Integer.parseInt(st.nextToken());
				str = "";
				for(j = 0; j < n; j++) {
					x = Integer.parseInt(st.nextToken());
					y = Integer.parseInt(st.nextToken());
					str += cipherText[x][y];
				}
				sb.append(str + "\n");
			}

			System.out.print(sb);
		}
		catch(Exception ex) {
			System.err.println(ex.toString());
		}
	}	
	
}